package com.xiaobao.coalproperty.other.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.find.model.NewsListEntity;

import java.util.ArrayList;
import java.util.List;

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> {

    private Context mContext;
    private List<NewsListEntity.DataBean.ListBean> mAdapterData;

    public NewsAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public List<NewsListEntity.DataBean.ListBean> getAdapterData() {
        return mAdapterData;
    }

    public void setAdapterData(List<NewsListEntity.DataBean.ListBean> mAdapterData) {
        this.mAdapterData = mAdapterData;
        this.notifyDataSetChanged();
    }

    public void addAdapterData(List<NewsListEntity.DataBean.ListBean> mAdapterData) {
        if (this.mAdapterData == null) {
            this.mAdapterData = new ArrayList<>();
        }
        this.mAdapterData.addAll(mAdapterData);
        this.notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_adapter_news, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Glide.with(mContext).load(mAdapterData.get(position).getPic()).into(holder.ivPic);
        holder.tvTitle.setText(mAdapterData.get(position).getTitle());
        holder.tvContent.setText(mAdapterData.get(position).getContent());
        holder.tvTime.setText(mAdapterData.get(position).getDate());
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        if (mAdapterData == null) {
            return 0;
        }
        return mAdapterData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivPic;
        TextView tvTitle, tvContent, tvTime;

        public ViewHolder(View itemView) {
            super(itemView);
            ivPic = itemView.findViewById(R.id.item_news_pic);
            tvTitle = itemView.findViewById(R.id.item_news_title);
            tvContent = itemView.findViewById(R.id.item_news_content);
            tvTime = itemView.findViewById(R.id.item_news_time);
            itemView.setOnClickListener(itemOnClickListener);
        }
    }

    //copy item
    private onItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(onItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    private View.OnClickListener itemOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
            }
        }
    };

    public interface onItemClickListener {
        void onItemClick(View view, int position);
    }
}
